﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using IBuy.Entity;
using IBuy.DataAccessLayer;
using System.Collections.Generic;

namespace IBuy.IBuyWebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "Ibuy-clover")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class SellorService : System.Web.Services.WebService
    {
        private Product goods;

        public SellorService()
        {
            goods = new Product();
        }
        /// <summary>
        /// 返回所有的商品信息
        /// </summary>
        /// <returns>List<Goods>列表</returns>
        [WebMethod(Description = "查询所有的商品")]
        public List<Product> QueryAllProduct()
        {
            
            DataSet dataset = new DataSet();
            dataset = goods.GetAllGoods();
            return GetList(dataset);
        }
        /// <summary>
        /// 返回所有的商品信息
        /// </summary>
        /// <param name="CompanyName">string，公司名</param>
        /// <returns>List<Goods>列表</returns>
        [WebMethod(Description = "根据公司名查询所有的商品")]
        public List<Product> QueryProductByCompanyName(string CompanyName)
        {
            DataSet dataset = goods.GetGoodsByCompanyName(CompanyName);
            return GetList(dataset);
        }

        /// <summary>
        /// 返回所有的商品信息
        /// </summary>
        /// <param name="GoodsName">string，商品名</param>
        /// <returns>List<Goods>列表</returns>
        [WebMethod(Description = "根据商品名查询所有的商品")]
        public List<Product> QueryProductByName(string GoodsName)
        {
            DataSet dataset = goods.GetGoodsByName(GoodsName);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据类型名查询所有的商品")]
        public List<Product> QueryProductByType(string TypeName)
        {
            DataSet dataset = goods.GetGoodsByType(TypeName);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据价格查询所有的商品")]
        public List<Product> QueryProductByPrice(float Price,RangeType RangeType)
        {
            DataSet dataset = goods.GetGoodsByPrice(Price,RangeType);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据库存查询所有的商品")]
        public List<Product> QueryProductByStock(int count, RangeType RangeType)
        {
            DataSet dataset = goods.GetGoodsByStock(count, RangeType);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据发货时间查询所有的商品")]
        public List<Product> QueryProductByDelivery(int delivery, RangeType RangeType)
        {
            DataSet dataset = goods.GetGoodsByDelivery(delivery, RangeType);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据价格（区间）查询所有的商品")]
        public List<Product> QueryProductByPriceRange(float min, float max)
        {
            DataSet dataset = goods.GetGoodsByPrice(min, max);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据库存（区间）查询所有的商品")]
        public List<Product> QueryProductByStockRange(int min, int max)
        {
            DataSet dataset = goods.GetGoodsByStock(min, max);
            return GetList(dataset);
        }

        [WebMethod(Description = "根据发货时间（区间）查询所有的商品")]
        public List<Product> QueryProductByDeliveryRange(int min, int max)
        {
            DataSet dataset = goods.GetGoodsByDelivery(min, max);
            return GetList(dataset);
        }
        [WebMethod(Description = "购买商品")]
        public bool BuyProduct(int ProductID, int Count)
        {
            return Product.BuyProducts(ProductID, Count);
        }
        /// <summary>
        /// 将Dataset中数据项转化成Goods对象，并将保存到List中，并将List作为返回值返回
        /// </summary>
        /// <param name="dataset">DataSet，要转换的dataset</param>
        /// <returns>List<Goods>，Goods对象集合</returns>
        private List<Product> GetList(DataSet dataset)
        {
            List<Product> list = new List<Product>();

            foreach (DataRow row in dataset.Tables[0].Rows)
            {
                Product goods = new Product();
                goods.CompanyName = row[0].ToString();
                goods.Type = row[1].ToString();
                goods.Name = row[2].ToString();
                goods.Price = float.Parse(row[3].ToString());
                goods.Stock = int.Parse(row[4].ToString());
                goods.Delivery = int.Parse(row[5].ToString());
                goods.ProductionID = int.Parse(row[6].ToString());
                list.Add(goods);
            }
            return list;
        }

        [WebMethod(Description = "根据代理商名查询折扣")]
        public List<DisCount> GetDiscount(string AgencyName)
        {
            List<DisCount> list = new List<DisCount>();
            DisCount dis = new DisCount(AgencyName);
            try
            {
                DataSet dataset = dis.GetDiscountByAgencyName(AgencyName);
                foreach (DataRow item in dataset.Tables[0].Rows)
                {
                    DisCount discount = new DisCount();
                    discount.GoodsName = item[0].ToString();
                    discount.AgencyName = item[1].ToString();
                    discount.Discount = Convert.ToSingle(item[2].ToString());
                    list.Add(discount);
                }
                
            }
            catch (Exception e)
            {
                list = null;
            }
            return list;
        }
    }
}
